package k1_jdbc.demo02.personOperation.daoImpl;

import k1_jdbc.demo02.personOperation.dao.PersonDao;
import k1_jdbc.demo02.personOperation.pojo.Person;
import k1_jdbc.tool.ConnectionTool;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PersonDaoImpl implements PersonDao {

    /**
     * 同时添加多个Person对象的数据
     * @param people
     * @return
     */
    @Override
    public boolean insert(List<Person> people) {

        List<Object[]> peopleArgs = new ArrayList<>();
        for (Person person : people) {
            peopleArgs.add(new Object[]{
                    person.getPersonId(),
                    person.getPersonName(),
                    person.getDegree(),
                    person.getBirth(),
                    person.getSal(),
                    "0"
            });
        }

        return ConnectionTool.executeUpdateSql(
                " insert into people values(?,?,?,?,?,?)",
                peopleArgs
        );
    }

    @Override
    public boolean update(List<Person> people) {
        List<Object[]> peopleArgs = new ArrayList<>();
        for (Person person : people) {
            peopleArgs.add(new Object[]{
                    person.getPersonName(),
                    person.getDegree(),
                    person.getBirth(),
                    person.getSal(),
                    person.getPersonId()
            });
        }

        return ConnectionTool.executeUpdateSql(
                "update people set personName=?,degree=?,birth=?,sal=? where personId=?",
                peopleArgs
        );
    }

    @Override
    public boolean delete(List<String> ids) {
        List<Object[]> peopleArgs = new ArrayList<>();
        for (String id : ids) {
            peopleArgs.add(new Object[]{id});
        }
        return ConnectionTool.executeUpdateSql("update people set isDelete='1' where personId=?",peopleArgs);
    }

    @Override
    public List<Person> getAll() {

        List<Person> people = new ArrayList<>();
        ResultSet rs = ConnectionTool.executeQuerySql("select * from people where isDelete='0'");

        try {
            while(rs.next()){
                people.add(new Person(
                    rs.getString(1),
                    rs.getString(2),
                    rs.getString(3),
                    rs.getString(4),
                    rs.getDouble(5)
                ));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return people;
    }

    @Override
    public List<Person> getPersonForPage(int pagesize, int pageOrder) {
        List<Person> people = new ArrayList<>();
        ResultSet rs = ConnectionTool.executeQuerySql(
                "select * from people where isDelete='0' limit ?,?",
                pageOrder*(pagesize-1),
                pageOrder
        );

        try {
            while(rs.next()){
                people.add(new Person(
                    rs.getString(1),
                    rs.getString(2),
                    rs.getString(3),
                    rs.getString(4),
                    rs.getDouble(5)
                ));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return people;
    }

    @Override
    public Person getPersonById(String id) {

        Person person = null;

        ResultSet rs = ConnectionTool.executeQuerySql("select * from people where isDelete='0' and personId=?",id);
        try {
            if(rs.next()){
                person = new Person(
                    rs.getString(1),
                    rs.getString(2),
                    rs.getString(3),
                    rs.getString(4),
                    rs.getDouble(5)
                );
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return person;

    }
}
